package org.anddev.andengine.extra;

import org.anddev.andengine.entity.layer.Layer;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnAreaTouchListener;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.input.touch.TouchEvent;

public abstract class ExtraScene extends Scene implements IExtraScene, IOnAreaTouchListener, IOnSceneTouchListener {
	
	public static int BACKGROUND_LAYER = 0;
	public static int GAME_LAYER = 1;
	public static int EXTRA_GAME_LAYER = 2;
	public static int SCORE_LAYER = 3;
	public static int FADE_LAYER = 4;
	
	public ExtraScene() {
		super(0);
		
		attachChild(new Layer());
		attachChild(new Layer());
		attachChild(new Layer());
		attachChild(new Layer());
		attachChild(new FadeLayer());
		
        setOnAreaTouchListener(this);
        setOnSceneTouchListener(this);
        
        createScene();
        ((FadeLayer) getChild(FADE_LAYER)).fadeOut();
	}
	
	public void replaceLayer(final int pLayerID, final Layer pLayer) {
		detachChild(getChild(pLayerID));
		insertChild(pLayerID, pLayer);
	}
	
	public void setFadeDelay(final float pDelay) {
		((FadeLayer) getChild(FADE_LAYER)).setFadeDelay(pDelay);
	}
	
	public void setFadeDuration(final float pDuration) {
		((FadeLayer) getChild(FADE_LAYER)).setFadeDuration(pDuration);
	}
	
	public void fadeIn() {
		((FadeLayer) getChild(FADE_LAYER)).fadeIn();
	}
	
	public void fadeOut() {
		((FadeLayer) getChild(FADE_LAYER)).fadeOut();
	}
	
	public void show() {
		((FadeLayer) getChild(FADE_LAYER)).show();
	}
	
	public void hide() {
		((FadeLayer) getChild(FADE_LAYER)).hide();
	}
	
	public void setAlpha(final float pAlpha) {
		((FadeLayer) getChild(FADE_LAYER)).setAlpha(pAlpha);
	}
	
	@Override
	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
		if (pSceneTouchEvent.isActionDown()) {
			manageAreaTouch(pTouchArea);
			return true;
		}
		return false;
	}
	
	@Override
	public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
		if (pSceneTouchEvent.isActionDown()) {
			manageSceneTouch(pSceneTouchEvent);
			return true;
		}
		return false;
	}
	
}
